import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios'

Vue.use(VueRouter)


const routes = [
  { // 定义组件的路径
    path: '*',
    name: '404',
    component: () => import('@/components/404.vue'),
  }, { // 登录
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/indexcopy.vue'),
  },{ // 登录
    path: '/appregister',
    name: 'appregister',
    component: () => import('@/components/app/appregister.vue'),
  },{ // 定义组件的路径
    path: '/speech',
    name: 'speech',
    component: () => import('@/components/speech/speech.vue'),
  },{ // 定义组件的路径
    path: '/datav',
    name: 'datav',
    component: () => import('@/components/datav/datav.vue'),
  },{ // 定义组件的路径
    path: '/phone',
    name: 'phone',
    component: () => import('@/components/test/phone.vue'),
  },{ // 定义组件的路径
    path: '/test',
    name: 'test',
    component: () => import('@/components/test/test.vue'),
  },{ // app登录
    path: '/applogin',
    name: 'applogin',
    component: () => import('@/components/app/applogin.vue'),
  },
  { // app端
    path: '/app',
    name: 'app',
    component: () => import('@/components/app/appindex.vue'),
    redirect: '/apphome',
    children: [
      {// app首页
        path: '/apphome',
        name: 'apphome',
        component: () => import('@/components/app/apphome.vue'),
      },{// app分类页
        path: '/appCategory',
        name: 'appCategory',
        component: () => import('@/components/app/appCategory.vue'),
      },{// app我的
        path: '/appPerson',
        name: 'appPerson',
        component: () => import('@/components/app/appPerson.vue'),
      },{// app购物车
        path: '/cart',
        name: 'cart',
        component: () => import('@/components/app/cart.vue'),
      },{// 评论
        path: '/comment',
        name: 'comment',
        component: () => import('@/components/app/comment.vue'),
      },{// 发评论
        path: '/submitComment',
        name: 'submitComment',
        component: () => import('@/components/app/submitComment.vue'),
      },
    ]
  },
  {// app商品页
    path: '/approduct',
    name: 'approduct',
    component: () => import('@/components/app/approduct.vue'),
  },{// app商品详情页
    path: '/productDetail',
    name: 'productDetail',
    component: () => import('@/components/app/productDetail.vue'),
  },{// app订单页
    path: '/appOrder',
    name: 'appOrder',
    component: () => import('@/components/app/appOrder.vue'),
  },{// app订单页多订单
    path: '/appOrderMore',
    name: 'appOrderMore',
    component: () => import('@/components/app/more/appOrderMore.vue'),
  },{// app地址页
    path: '/address',
    name: 'address',
    component: () => import('@/components/app/address.vue'),
  },{// app地址编辑
    path: '/AddressEdit',
    name: 'AddressEdit',
    component: () => import('@/components/app/AddressEdit.vue'),
  },{// app全部订单
    path: '/order',
    name: 'order',
    component: () => import('@/components/app/order.vue'),
  },{// app订单详情页
    path: '/orderDetail',
    name: 'orderDetail',
    component: () => import('@/components/app/orderDetail.vue'),
  },{// app头像页
    path: '/aAvatar',
    name: 'aAvatar',
    component: () => import('@/components/app/mine/aAvatar.vue'),
  },]


const router = new VueRouter({
  routes
});

export const resetRouter = () => {
  router.matcher = new VueRouter({
    routes
  })
}

export const setRoutes = () => {
  if (localStorage.getItem("menus") == null) {
    router.push('/frontindex');
    return;
  }
  let storeMenus = JSON.parse(localStorage.getItem("menus"));

  console.log(storeMenus)
  if (storeMenus.length>0) {
    let redir=storeMenus[0].path
    if (!storeMenus[0].path){
      redir=storeMenus[0].children[0].path
    }
    const manageRoute = {
      path: '/index',
      name: 'index',
      component: () => import('@/components/index.vue'),
      redirect: redir,
      children: []
    }
    storeMenus.forEach(item => {
      if (item.path) {
        let itemMenu = {
          path: item.path,
          name: item.name,
          component: () => import('@/components/' + item.pagePath + '.vue')
        }
        manageRoute.children.push(itemMenu)
      } else if (item.children.length) {
        item.children.forEach(item => {
          if (item.path) {
            let itemMenu = {
              path: item.path,
              name: item.name,
              component: () => import('@/components/' + item.pagePath + '.vue')
            }
            manageRoute.children.push(itemMenu)
          }
        })
      }
    })
    let currentRoute = router.getRoutes().map(v => v.name);
    if (!currentRoute.includes('index')) {
      router.addRoute(manageRoute)
    }
  }else {
    const manageRoute = {
      path: '/index',
      name: 'index',
      component: () => import('@/components/index.vue'),
      redirect: '/404',
      children: []
    }
    router.addRoute(manageRoute)
  }
  // console.log(router.getRoutes())
}
setRoutes()

//
// router.beforeEach((to,from,next)=>{
//   window.scrollTo(0,0);
//   if(to.name==='login'){
//     next()
//   }else{
//     let url='http://localhost:9090/iflogin'
//     request.get(url,{
//       headers: {
//         'content-type': 'application/json',
//         "token": localStorage.getItem("token")  //token换成从缓存获取
//       }
//     }).then(resp=>{
//       let result=resp.data;
//       if(result.code===200){
//         if(to.name==='backindex'||to.name==='home'||to.name==='carmanager'||to.name==='usermanager'||to.name==='park'||to.name==='order'||to.name==='notice'){
//         if(result.data.role==="2"){
//           next()
//         }else{
//           next({name:"index"})
//         }
//       }else if (to.name==='index'||to.name==='myorder'||to.name==='booking'||to.name==='carparking'){
//           if(result.data.role==="1"){
//             next()
//           }else{
//             next({name:"backindex"})
//           }
//       }
//     }else if(result.code===0){
//        next({name:"login"})
//     }
//
//     })
//   }
//
//
//
// })


export default router
